package com.easywan.interceptor;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.easywan.service.IpSerivce;
import com.easywan.vo.Ipcounter;

@Component
public class IpCounterInterceptor implements HandlerInterceptor{
	private static Logger logger = Logger.getLogger(IpCounterInterceptor.class);
	@Autowired
	private IpSerivce ipService;
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object action) throws Exception {
		String ipAddr = request.getRemoteHost();
		String os = request.getHeader("user-agent");
		String url = request.getRequestURI();
		Ipcounter ip = new Ipcounter();
		ip.setIpaddress(ipAddr);
		ip.setOs(os);
		ip.setUrl(url);
		ipService.addIp(ip);
		logger.info(ip);
		return true;
	}
	
}
